var whois = require('./whoisclient2.js');
var request = require('request');
var is = require('is');
// 域名组成
var charArray = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
var numberArray = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
var symbolArray = ['-'];
var mixArray = [];
mixArray = mixArray.concat(charArray);
// 域名长度限制
var MAX_LENGTH = 128;
var MAX_WORKERS = 8;
var workers = 0;
// ['cloud', 'data', 'game', 'edu', 'health', 'medical', 'media', 'smart', 'home', 'yun']
var fixedArray = ['data'];
// ['com', 'net', 'cc', 'io']
var domainExArray = ['com', 'net'];
var luckyDomain = [];
var domainArray = [];
var running = true;
for (var key in fixedArray) {
	domainArray = domainArray.concat(generateDomain(1, fixedArray[key], 0));
}
var domainArrayLen = domainArray.length;

function generateDomain(preLen, fixed, sufLen) {
	var result = [];
	var preArray = [];
	var sufArray = [];
	var tmp = [];
	var tmptmp = [];
	if (!is.int(preLen)) {
		preLen = 0;
	}
	if (!is.int(sufLen)) {
		sufLen = 0;
	}
	if (!is.string(fixed)) {
		fixed = '';
	}
	// 生成前缀数组
	tmp = mixArray;
	for (var i = 0; i < preLen; i++) {
		for (var prepre in mixArray) {
			for (var pre in preArray) {
				tmp.push(mixArray[prepre] + preArray[pre]);
			}
		}
		preArray = tmp;
		tmp = [];
	}
	// 生成后缀数组
	tmp = mixArray;
	for (var i = 0; i < sufLen; i++) {
		for (var sufsuf in mixArray) {
			for (var suf in sufArray) {
				tmp.push(mixArray[sufsuf] + sufArray[suf]);
			}
		}
		sufArray = tmp;
		tmp = [];
	}
	// 组合前缀和固定部分
	tmp = [];
	for (var pre in preArray) {
		tmp.push(preArray[pre] + fixed);
	}
	if (preArray.length == 0) {
		tmp.push(fixed);
	}
	// 组合临时和后缀
	for (var suf in sufArray) {
		for (var dom in tmp) {
			tmptmp.push(tmp[dom] + sufArray[suf]);
		}
	}
	if (sufArray.length == 0) {
		tmptmp = tmp;
	}
	// 组合临时临时和扩展
	for (var ex in domainExArray) {
		for (var dom in tmptmp) {
			result.push(tmptmp[dom] + '.' + domainExArray[ex]);
		}
	}
	return result;
}

function getDomain() {
	return domainArray.shift();
}

function requestWhois(domain) {
	if (is.empty(domain)) {
		console.log('[err] 域名不能为空');
		return;
	}
	workers++;
	//console.log('workers++  ' + workers);
	whois.query(domain, function (data) {
		if (data.indexOf('Creation Date: ') != -1) {
			// 域名已注册
			var createTime = data.substr(data.indexOf('Creation Date: ') + 15, 11);
			console.log('[suc]  ' + domain + '  ' + createTime);
		} else {
			luckyDomain.push(domain);
			if (data.indexOf('Socket timeout.') != -1) {
				// 网络超时
				console.log('[err]  ******  ' + domain + '  请求超时');
			} else {
				// 域名未注册
				console.log('[suc]  ******  ' + domain + '  域名未注册');
			}
		}
		workers--;
	});
}

(function loop() {
	if (workers < MAX_WORKERS) {
		var domain = getDomain();
		if (is.empty(domain)) {
			running = false;
			console.log('All domain is ba~ la~ la~ la~, I will sleep in 60s ...');
			setTimeout(function () {
				process.exit(0);
			}, 60000);
		} else {
			requestWhois(domain);
		}
	}
	if (running) {
		setTimeout(function () {
			loop();
		}, 1);
	}
})();

process.on('exit', function (code) {
	console.log('');
	console.log('============================');
	console.log('     luckyDomain v1.1.0     ');
	console.log(luckyDomain);
	console.log('');
	console.log(luckyDomain.length + '/' + domainArrayLen + ' domains');
	console.log('============================');
	console.log('');
});
